<?php  
	echo "<div id='text'>
			<div class='title'>".bread_crumbs(NULL, TRUE)." My Reservations - ".$_SESSION[''.SECURITY_SESSION.'']['username']."</div>
		  </div>";	
		  
	if (isset($_SESSION[''.SECURITY_SESSION.'']['user_id'])){
		$cancel = isset($_REQUEST['cancel_reservation']) ? $_REQUEST['cancel_reservation'] : NULL;
		$reserve_id = isset($_REQUEST['reservation_id']) ? $_REQUEST['reservation_id'] : NULL;
		
		if($cancel)
			api_call("cancel_reservation", array('reservation_id' => $reserve_id));
	
		$my_reserves = api_call("get_my_reservations", array('user_id' => $_SESSION[''.SECURITY_SESSION.'']['user_id']));
		$numOfReserves = count($my_reserves['method_result']);
		
		echo "<div class='institution_title2'>My Reservations from ".ISLAND_NAME."</div>";
		
			echo "<table id='reserve' class='table_max'>
					<tr class='top'>
						<td>ID</td>
						<td>Begin Time</td>
						<td>End Time</td>
						<td>Actions</td>
					</tr>";
					
		if($numOfReserves == 0)
			echo "	<tr>
						<td colspan='4'>You Have not a Reservation</td>
					</tr>";
		
		for($i = 0; $i < $numOfReserves; $i++) {
			if(empty($my_reserves['method_result'][$i]['resource_list']))
				continue;
				
			echo "	<tr>
						<td>".$my_reserves['method_result'][$i]['id']."</td>
						<td>".$my_reserves['method_result'][$i]['begin_time']."</td>
						<td>".$my_reserves['method_result'][$i]['end_time']."</td>
						<td>
							<form method='POST' action='' style='display: inline;'>
								<input type='hidden' name='reservation_id' value='".$my_reserves['method_result'][$i]['id']."'>
								<input type='hidden' name='cancel_reservation' value='Remove'>
								<input type='button' value='Remove' onClick='confirmRemove(\"Do you really want to remove this reservation?\", this)'>
							</form>
							<input type='submit' value='Details' onclick='showReservationDetails(".$i.")'>
						</td>
					</tr>";
			
			$resources = api_call("get_resources", array('filter' => $my_reserves['method_result'][$i]['resource_list']));
			
			$nodes = array();
			$channels = array();
			$resources_status = array('OK', 'Maintenance');
			
			for($j = 0; $j < count($resources['method_result']); $j++) {
				$resource_data = array(
								'resource_id' => $resources['method_result'][$j]['id'],
								'resource_details' => $resources['method_result'][$j]['resource_details'],
								'status' => $resources['method_result'][$j]['status']
							  );
				
				if($resources['method_result'][$j]['type'] == 'node')
					$nodes[] = $resource_data;
				else
					$channels[] = $resource_data;
			}
			
			// Display Resources from reservation
			echo "<tr class='hidden_resources' id='reservation_details_".$i."'>
					<td colspan='4'>
						<table id='reservation_resources'>
							<tr class='top'>
								<td>ID</td>
								<td>Resource</td>
								<td>Actual status</td>
							</tr>";
			
			for( $j = 0; $j < count($nodes); $j++ ) {
				$node_description = "<center><b>Node description:</b></center>" .
									"<b>Hostname:</b> ".$nodes[$j]['resource_details']['hostname']."<br>" .
									"<b>Control IP:</b> ".$nodes[$j]['resource_details']['control_ip']."<br>" .
									"<b>HRN:</b> ".$nodes[$j]['resource_details']['hrn']."";
				
				echo "<tr>
					<td>".$nodes[$j]['resource_id']."</td>
					<td><span onMouseOver=\"toolTip('".$node_description."', 200)\" onMouseOut=\"toolTip()\">".$nodes[$j]['resource_details']['hostname']."</span></td>
					<td>".$resources_status[$nodes[$j]['status']]."</td>
				</tr>";
			}
			
			for( $j = 0; $j < count($channels); $j++ ) {
				echo "<tr>
					<td>".$channels[$j]['resource_id']."</td>
					<td>Channel ".$channels[$j]['resource_details']['channel_number']."</td>
					<td>".$resources_status[$channels[$j]['status']]."</td>
				</tr>";
			}
			
			echo "  	</table>
					</td>
				  </tr>";
		}
		
		echo "</table>";
		
	}else
		set_message('You do not have a slice! Please contact LABORA for a slice creation!','error',1);
?>		
